package com.cbw.controller;

import com.cbw.bean.*;
import com.cbw.mvc.ResponseBody;
import com.cbw.service.CourierService;
import com.cbw.service.UserService;
import com.cbw.util.DateFormatUtil;
import com.cbw.util.JSONUtil;

import javax.json.Json;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Controller主要操作:
 * 1.接收参数
 * 2.封装参数
 * 3.调用service，处理请求
 * 4.填充消息对象
 * 5.处理json
 * 6.返回json
 */
public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //接收参数 无
        //调用service
        List<Map<String, Integer>> list = UserService.console();//list中存放了一个map,map中存放两个键值对size day_num
        //填充消息对象
        Message msg = new Message();
        if (list.size()>0){
            msg.setStatus(0);
            msg.setData(list);
        }else {
            msg.setStatus(-1);
        }
        //处理json
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始分页查询");
        //获取参数:分页查询的起始索引、每页记录数
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //调用service,分页查询
        List<User> list = UserService.findALl(true, offset, pageNumber);

        //将Courier改成BootStrapTableCourier类型，将除了int外其它类型改为string
        ArrayList<BootStrapTableUser> list2 = new ArrayList<>();
        for (User user : list) {
            String createTime = DateFormatUtil.format(user.getCreateTime());
            String lasTime=user.getLastTime()==null?"尚未登录":DateFormatUtil.format(user.getLastTime());
            //int id, String number, String username, String userPhone, String password, Timestamp createTime, Timestamp lastTime, String cardId,boolean isUser
            BootStrapTableUser c2 = new BootStrapTableUser(user.getId(), user.getNumber(), user.getUsername(), user.getUserPhone(), user.getPassword(),createTime,lasTime,user.getCardId());

            list2.add(c2);
        }
        System.out.println("分页查询结果:"+list2);

        //创建分页对象
        ResultData<BootStrapTableUser> data = new ResultData<>();
        //填充分页对象(分页数据，总数量)
        data.setRows(list2);
        data.setTotal(UserService.total());
        //json处理
        String json = JSONUtil.toJSON(data);
        System.out.println("分页查询数据:"+json);
        return json; //返回给DisPatchServlet->页面ajax
        //测试:http://localhost:8080/user/list.do?offset=0&pageNumber=5
    }
    @ResponseBody("/user/find.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        String userPhone = request.getParameter("userPhone");
        //调用service，查询
        User user = UserService.findUserByUserPhone(userPhone);
        System.out.println("查询的用户:"+user);
        //填充消息对象
        Message msg = new Message();
        if (user!=null){
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);

        }else {
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        }
        //json处理
        String json = JSONUtil.toJSON(msg);
        System.out.println("查询到的json："+json);
        return json;
        //测试:http://localhost:8080/user/find.do?userPhone=18751902567
    }

    @ResponseBody("/user/add.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setUserPhone(request.getParameter("userPhone"));
        user.setCardId(request.getParameter("cardId"));
        user.setPassword(request.getParameter("password"));
        System.out.println("待插入数据:"+user);
        //user.setLastTime((Timestamp) new Date());
        //插入
        boolean insert = UserService.insert(user);
        //填充消息对象
        Message msg = new Message();
        if (insert){
            msg.setStatus(0);
            msg.setResult("插入成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("插入失败");
        }
        //处理json
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        User user = new User();
        user.setId(Integer.parseInt(request.getParameter("id")));
        user.setUsername(request.getParameter("username"));
        user.setUserPhone(request.getParameter("userPhone"));
        user.setCardId(request.getParameter("cardId"));
        user.setPassword(request.getParameter("password"));
        //更新
        boolean update = UserService.update(user);
        //填充消息对象
        Message msg = new Message();
        if (update){
            msg.setStatus(0);
            msg.setResult("更新成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        //处理json
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        int id = Integer.parseInt(request.getParameter("id"));
        //删除
        boolean delete = UserService.delete(id);
        //填充消息对象
        Message msg = new Message();
        if (delete){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        //处理json
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
